package com.yiwenup.leetcode.site;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.site
 * @Author yiwenup
 * @Date 2024-12-15 22:07:50
 * @Description https://leetcode.cn/problems/intersection-of-two-arrays/
 */
public class No0349 {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> tmp = new HashSet<>();

        boolean[] numbers = new boolean[1_001];
        for(Integer num : nums1) {
            numbers[num] = true;
        }
        for(Integer num : nums2) {
            if(numbers[num]) {
                tmp.add(num);
            }
        }

        int[] ans = new int[tmp.size()];
        Iterator<Integer> iter = tmp.iterator();
        for(int i = 0; i < ans.length; i++) {
            ans[i] = iter.next();
        }

        return ans;
    }

    /*public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> tmp = new HashSet<>();

        Set<Integer> numbers = new HashSet<>();
        for(Integer num : nums1) {
            numbers.add(num);
        }
        for(Integer num : nums2) {
            if(numbers.contains(num)) {
                tmp.add(num);
            }
        }

        int[] ans = new int[tmp.size()];
        Iterator<Integer> iter = tmp.iterator();
        for(int i = 0; i < ans.length; i++) {
            ans[i] = iter.next();
        }

        return ans;
    }*/

    public static void main(String[] args) {
        new No0349().intersection(new int[]{1, 2, 2, 1}, new int[]{2, 2});
    }
}
